//第三层持久层

const mongoose = require('mongoose');

module.exports.add3 = async function(data){
    return await mongoose.model('adminModel').create(data);
}
//搜索获取
module.exports.search3 = async function({searchType,searchValue,currentPage,pageSize}){
    const total = await mongoose.model('adminModel').find({[searchType]:{$regex:searchValue,$options:'$i'}})//获取总数
    const pages = Math.ceil(total.length / pageSize);
    const adminData = await mongoose.model('adminModel')
    .find({[searchType]:{$regex:searchValue,$options:'$i'}})
    .limit(pageSize - 0)
    .skip((currentPage - 1) * pageSize)
    return {pages,adminData}
}

//普通获取
module.exports.get3 = async function({currentPage,pageSize}){
    const total = await mongoose.model('adminModel').countDocuments();//获取总数
    const pages = Math.ceil(total / pageSize);
     const adminData = await mongoose.model('adminModel')
    .find()
    .limit(pageSize - 0)
    .skip((currentPage - 1) * pageSize)
    return {pages,adminData}
}

module.exports.delete3 = async function({_id,admin_del}){
    return await mongoose.model('adminModel').updateOne({_id},{admin_del});
}

module.exports.getOne3 = async function(data){
    return await mongoose.model('adminModel').find(data)
}

module.exports.updataOne3 = async function({_id,admin_id,admin_status,admin_name,admin_loginName,admin_pass,admin_phone}){
    return await mongoose.model('adminModel').updateOne({_id},{admin_id,admin_status,admin_name,admin_loginName,admin_pass,admin_phone});
}